<template>
    <!-- 基于时延的重定位任务 -->
    <div class="app-container">
        <el-form :model="queryParams" ref="queryParams" size="small" :inline="true" v-show="showSearch"
            label-width="100px">
            <el-form-item label="受保护IP" prop="protectedIp">
                <el-input v-model="queryParams.protectedIp"></el-input>
            </el-form-item>
            <el-form-item label="任务状态" prop="taskState">
                <el-select v-model="queryParams.taskState">
                    <el-option v-for="dict in dict.type.anti_taskstate" :value="dict.value"
                        :label='dict.label'></el-option>
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleSearch">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>
        <el-row :gutter="10">
            <!-- <el-col :span="1.5">
          <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
            v-hasPermi="['system:notice:add']">新增</el-button>
        </el-col> -->
            <el-col :span="1.5">
                <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="!selectedRows.length"
                    @click="handleBatchDelete" v-hasPermi="['system:notice:remove']">删除</el-button>
            </el-col>
            <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
                v-hasPermi="['system:notice:export']">导出</el-button>
        </el-row>
        <el-table ref="multipleTable" :data="tableData" tooltip-effect="dark" style="width: 100%"
            :default-sort="{ prop: 'date', order: 'descending' }" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" />
            <el-table-column prop="protectedIp" label="重定位设备IP" show-overflow-tooltip sortable />
            <!-- <el-table-column prop="beforeLon" label="重定位前坐标" show-overflow-tooltip>
                <template slot-scope="scope">
                    {{ scope.row.beforeLon }},{{ scope.row.beforeLat }}
                </template>
            </el-table-column> -->
            <el-table-column prop="afterLon" label="重定位后经度" show-overflow-tooltip sortable />
            <el-table-column prop="afterLat" label="重定位后纬度" show-overflow-tooltip sortable />
            <!-- <el-table-column prop="beforeLon" label="重定位后坐标" show-overflow-tooltip>
                <template slot-scope="scope">
                    {{ scope.row.afterLon }},{{ scope.row.afterLat }}
                </template>
            </el-table-column> -->
            <!-- <el-table-column prop="beforeDistance" label="重定位前风险距离(KM)" show-overflow-tooltip sortable
                width="180" /> -->
            <el-table-column prop="afterDistance" label="重定位后风险距离(KM)" show-overflow-tooltip sortable width="180" />
            <el-table-column prop="taskState" label="任务状态" show-overflow-tooltip>
                <template slot-scope="scope">
                    <span v-for="dict in dict.type.anti_taskstate">
                        <span v-if="scope.row.taskState == dict.value">
                            {{ dict.label }}
                        </span>
                    </span>
                </template>
            </el-table-column>
            <el-table-column prop="startime" label="任务开始时间" show-overflow-tooltip />
            <el-table-column prop="endtime" label="任务结束时间" show-overflow-tooltip />
            <el-table-column prop="operation" label="操作" width="250" show-overflow-tooltip>
                <template slot-scope="scope">
                    <!-- <el-button type="text" @click="handleDetail(scope.row)">
                        详情
                    </el-button> -->
                    <el-button type="text" @click="handleDelete(scope.row)" style="color: red">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentPageChange"
            :current-page="queryParams.pageNum" :page-size="queryParams.pageSize" :page-sizes="[10, 20, 50, 100]"
            :total="total" layout="total, sizes, prev, pager, next, jumper"
            style="margin-top: 20px; text-align: center;">
        </el-pagination>
        <!-- 删除弹窗 -->
        <el-dialog title="提示" :visible.sync="dialogVisible" width="30%">
            <span>确认删除这些任务吗？</span>
            <span slot="footer" class="dialog-footer">
                <el-button @click="dialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="dialogVisible = false">确 定</el-button>
            </span>
        </el-dialog>
        <!-- 新增编辑 -->
        <el-dialog :title="title + '任务'" :visible.sync="addDialogVisible" width="50%">
            <div class="apk-detail-content">
                <el-card class="box-card">
                    <el-form ref="formEquip" :model="formEquip" :rules="rules" label-width="120px">
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="受保护IP" prop="protectedIp">
                                    <el-input v-model="formEquip.protectedIp"></el-input>
                                </el-form-item>
                                <el-form-item label="抗定位方法" prop="taskMethod">
                                    <el-input v-model="formEquip.taskMethod"></el-input>
                                </el-form-item>
                                <el-form-item label="任务状态" prop="taskState">
                                    <el-input v-model="formEquip.taskState"></el-input>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="任务开始时间" prop="startime">
                                    <el-date-picker v-model="formEquip.startime" align="right" type="date"
                                        placeholder="选择日期" value-format="yyyy-MM-dd">
                                    </el-date-picker>
                                </el-form-item>
                                <el-form-item label="任务结束时间" prop="endtime">
                                    <el-date-picker v-model="formEquip.endtime" align="right" type="date"
                                        placeholder="选择日期" value-format="yyyy-MM-dd">
                                    </el-date-picker>
                                </el-form-item>
                                <el-form-item label="任务执行结果" prop="taskResult">
                                    <el-input v-model="formEquip.taskResult"></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-form-item>
                            <el-button type="primary" @click="onSubmit" size="small">提交</el-button>
                            <el-button size="small" @click="cancel">取消</el-button>
                        </el-form-item>
                    </el-form>
                </el-card>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import { listEquipTask, addEquipTask, updateEquipTask, delEquipTask, listEquipRepositingTask, updateCombatEquipRepositingTask, deleteCombatEquipRepositingTask } from '@/api/networkEquipment/antiLocalization'
export default {
    dicts: ["equipment_risklevel", "anti_methods", "anti_taskstate"],
    data() {
        return {
            activeName: "first",
            dialogVisible: false,
            dialogVisible1: false,
            currentPage: 1, // 当前页码
            pageSize: 10, // 每页显示的数据条数
            totalDataCount: 0, // 总数据量，初始为0，将在数据加载时设置
            selectedRows: [], // 存放选中的行
            tableData: [],
            showSearch: true,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                noticeTitle: undefined,
                createBy: undefined,
                status: undefined
            },
            total: 0,
            addDialogVisible: false,
            formEquip: {},
            rules: {
                protectedIp: [
                    { required: true, message: '请输入设备名称', trigger: 'blur' },
                ],
            },
            title: "",
        };
    },
    methods: {
        // 编辑
        handleEdit(row) {
            console.log(row);
            this.title = '编辑'
            this.addDialogVisible = true;
            this.formEquip = JSON.parse(JSON.stringify(row))
        },
        // 取消
        cancel() {
            this.resetForm("formEquip");
            this.addDialogVisible = false
        },
        // 提交
        onSubmit() {
            this.$refs['formEquip'].validate((valid) => {
                if (valid) {
                    if (this.title == '新增') {
                        addEquipTask(this.formEquip).then(res => {
                            if (res.code == 200) {
                                this.getList()
                                this.addDialogVisible = false
                                this.$message({
                                    type: 'success',
                                    message: '新增成功!',
                                });
                            }
                        })
                    } else {
                        updateEquipTask(this.formEquip).then(res => {
                            if (res.code == 200) {
                                this.getList()
                                this.addDialogVisible = false
                                this.$message({
                                    type: 'success',
                                    message: '编辑成功!',
                                });
                            }
                        })
                    }
                } else {
                    console.log('error submit!!');
                    return false;
                }
            });

        },
        // 新增
        handleAdd() {
            this.resetForm("formEquip");
            this.addDialogVisible = true;
            this.title = "新增";
        },
        // 重置
        resetQuery() {
            this.resetForm('queryParams')
            this.getList()
        },
        // 检索
        handleSearch() {
            this.queryParams.pageNum = 1
            this.queryParams.pageSize = 10
            this.getList()
        },
        // 获取列表
        getList() {
            listEquipRepositingTask(this.queryParams).then(res => {
                this.tableData = res.rows || []
                this.total = res.total
            })
        },
        handleClick() {
            // 处理选项卡点击事件
        },

        handleSelectionChange(rows) {
            this.selectedRows = rows;
        },

        handleDownload() {
            // 处理下载按钮点击事件
        },
        handleSizeChange(newSize) {
            this.queryParams.pageNum = 1
            this.queryParams.pageSize = newSize
            this.getList()
        },
        handleCurrentPageChange(newPage) {
            this.queryParams.pageNum = newPage
            this.getList()
        },
        handleBatchDelete() {
            if (this.selectedRows.length === 0) {
                this.$message({
                    message: '请至少选择一个设备进行删除',
                    type: 'warning'
                });
                return;
            }

            this.$confirm('确认删除这些设备吗？', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                let ids = []
                this.selectedRows.map(ele => {
                    ids.push(ele.id)
                })
                deleteCombatEquipRepositingTask(ids).then(res => {
                    if (res.code == 200) {
                        this.getList()
                        this.$message({
                            type: 'success',
                            message: '批量删除成功!',
                        });
                    }
                })
            }).catch(() => {
                this.$message({
                    message: '已取消批量删除',
                    type: 'info'
                });
            });
        },
        handleDetail(row) {
            console.log('rowrowrowrow', row);
            this.$router.push({
                path: '/networkestimateDetail',
                query: { id: row.id }
            })
        },
        handleDelete(row) {
            this.$confirm('此操作将永久删除该设备, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                deleteCombatEquipRepositingTask(row.id).then(res => {
                    if (res.code == 200) {
                        this.getList()
                        this.$message({
                            type: 'success',
                            message: '删除成功!',
                        });
                    }
                })
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });
        },
        /** 导出按钮操作 */
        handleExport() {
            this.download('equip/info/export', {
                ...this.queryParams
            }, `application_${new Date().getTime()}.xlsx`)
        },
    },
    computed: {
        currentTableData() {
            let start = (this.currentPage - 1) * this.pageSize;
            let end = start + this.pageSize;
            return this.tableData.slice(start, end);
        },
    },
    mounted() {
        this.getList()
    },
};
</script>

<style lang="scss" scoped>
.mainTable {
    padding: 15px;
    height: 85vh;
    display: flex;
    justify-content: space-around;
    flex-direction: column;
}

.flex-container {
    display: flex;
    justify-content: space-between;
    align-items: center;
    /* 垂直居中对齐 */
}

.btn {
    margin-left: 60px;
    background: linear-gradient(228deg, #FFC04B 0%, #FFA216 100%);
    color: #fff;
    border-radius: 4px 4px 4px 4px;
}

.flex-group-right {
    margin-left: 15px;
}

.rightBtn {
    margin-right: 55px;
    width: 106px;
    background: linear-gradient(270deg, #2DBFFD 0%, #0E5FFF 100%);
    border-radius: 4px 4px 4px 4px;
}

.textStyle {
    margin-left: 5px;
    width: 96px;
    height: 22px;
    font-family: PingFang SC, PingFang SC;
    font-weight: 400;
    font-size: 16px;
    color: #535A6C;
    line-height: 19px;
    text-align: left;
    font-style: normal;
    text-transform: none;
}

::v-deep .el-pager li.active {
    background: linear-gradient(270deg, #2DBFFD 0%, #0E5FFF 100%) !important;
    color: white !important;
}

.search-icon {
    cursor: pointer;
    font-size: 18px;
    vertical-align: middle;
}

::v-deep .el-pagination {
    float: inline-end;
}
</style>